home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / irit / freefrm4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  31.0 KB  |  736 lines

  1. /*****************************************************************************
  2. *   "Irit" - the 3d (not only polygonal) solid modeller.             *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. ******************************************************************************
  6. *   Module to provide the required interfact for the cagd library for the    *
  7. * free form surfaces and curves.                         *
  8. *****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include "program.h"
  13. #include "allocate.h"
  14. #include "attribut.h"
  15. #include "objects.h"
  16. #include "primitiv.h"
  17. #include "windows.h"
  18. #include "ip_cnvrt.h"
  19. #include "freeform.h"
  20.  
  21. static IPObjectStruct *GetControlTVMesh(IPObjectStruct *LstLstObjList,
  22.                     int UOrder,
  23.                     int VOrder,
  24.                     int WOrder,
  25.                     TrivGeomType GType,
  26.                     char **ErrStr);
  27.  
  28. /*****************************************************************************
  29. * DESCRIPTION:                                                               M
  30. *   Construct a trimmed surfcae, given the parametric surface and its         M
  31. * trimming curves.                                 M
  32. *                                                                            *
  33. * PARAMETERS:                                                                M
  34. *   SrfObj:         To construct the trimmed surface with.                   M
  35. *   TrimmedCrvsObj: To trim srf with. No real validity testing is performed. M
  36. *            Can be NULL Create a structure of trimmed surface,       M
  37. *            untrimmed.                             M
  38. *   RHasTopLvlTrim: TRUE if needs to add a top level rectangular boundary.   M
  39. *                                                                            *
  40. * RETURN VALUE:                                                              M
  41. *   IPObjectStruct *:   A trimmed surface object.                            M
  42. *                                                                            *
  43. * KEYWORDS:                                                                  M
  44. *   GenTrimmedSurface                                                        M
  45. *****************************************************************************/
  46. IPObjectStruct *GenTrimmedSurface(IPObjectStruct *SrfObj,
  47.                   IPObjectStruct *TrimmedCrvsObj,
  48.                   RealType *RHasTopLvlTrim)
  49. {
  50.     CagdCrvStruct
  51.     *Crvs = NULL;
  52.     CagdBType
  53.     HasTopLvlTrim = REAL_PTR_TO_INT(RHasTopLvlTrim);
  54.  
  55.     if (IP_IS_CRV_OBJ(TrimmedCrvsObj)) {
  56.     Crvs = CagdCrvCopyList(TrimmedCrvsObj -> U.Crvs);
  57.     }
  58.     else if (IP_IS_OLST_OBJ(TrimmedCrvsObj)) {
  59.     int NumCrvs = 0;
  60.     IPObjectStruct *CrvObj;
  61.  
  62.     while ((CrvObj = ListObjectGet(TrimmedCrvsObj, NumCrvs++)) != NULL) {
  63.         CagdCrvStruct *Crv;
  64.  
  65.         if (!IP_IS_CRV_OBJ(CrvObj)) {
  66.         IritPrsrFatalError("Non curve object as a trimmed curve");
  67.         return NULL;
  68.         }
  69.  
  70.         Crv = CagdCrvCopy(CrvObj -> U.Crvs);
  71.         LIST_PUSH(Crv, Crvs);
  72.     }
  73.     }
  74.  
  75.     return GenTRIMSRFObject(TrimSrfNew2(CagdSrfCopy(SrfObj -> U.Srfs),
  76.                     Crvs, HasTopLvlTrim));
  77. }
  78.  
  79. /*****************************************************************************
  80. * DESCRIPTION:                                                               M
  81. *   Extracts the parametric surface of a trimmed surface.                    M
  82. *                                                                            *
  83. * PARAMETERS:                                                                M
  84. *   TrimmedSrfObj:   To extract parametric surface from.                     M
  85. *                                                                            *
  86. * RETURN VALUE:                                                              M
  87. *   IPObjectStruct *:    Extracted parametric surface.                       M
  88. *                                                                            *
  89. * KEYWORDS:                                                                  M
  90. *   GetSrfFromTrimmedSrf                                                     M
  91. *****************************************************************************/
  92. IPObjectStruct *GetSrfFromTrimmedSrf(IPObjectStruct *TrimmedSrfObj)
  93. {
  94.     return GenSRFObject(CagdSrfCopy(TrimmedSrfObj -> U.TrimSrfs -> Srf));
  95. }
  96.  
  97. /*****************************************************************************
  98. * DESCRIPTION:                                                               M
  99. *   Extracts trimming curves of trimming surface, either in parametric or    M
  100. * Euclidean space.                                                           M
  101. *                                                                            *
  102. * PARAMETERS:                                                                M
  103. *   TrimmedSrfObj:   To extract trimming curves from.                        M
  104. *   RParamSpace:     TRUE for parametric space, FALSE for Euclidean space.   M
  105. *                                                                            *
  106. * RETURN VALUE:                                                              M
  107. *   IPObjectStruct *:   Extracted trimming curves.                           M
  108. *                                                                            *
  109. * KEYWORDS:                                                                  M
  110. *   GetTrimCrvsFromTrimmedSrf                                                M
  111. *****************************************************************************/
  112. IPObjectStruct *GetTrimCrvsFromTrimmedSrf(IPObjectStruct *TrimmedSrfObj,
  113.                       RealType *RParamSpace)
  114. {
  115.     int Count = 0,
  116.     ParamSpace = REAL_PTR_TO_INT(RParamSpace);
  117.     CagdCrvStruct
  118.     *Crvs = TrimGetTrimmingCurves(TrimmedSrfObj -> U.TrimSrfs, ParamSpace);
  119.     IPObjectStruct
  120.     *ListObj = IPAllocObject("", IP_OBJ_LIST_OBJ, NULL);
  121.  
  122.     while (Crvs) {
  123.     CagdCrvStruct
  124.         *Crv = Crvs;
  125.  
  126.     Crvs = Crvs -> Pnext;
  127.     Crv -> Pnext = NULL;
  128.     ListObjectInsert(ListObj, Count++, GenCRVObject(Crv));
  129.     }
  130.  
  131.     ListObjectInsert(ListObj, Count, NULL);
  132.     return ListObj;
  133. }
  134.  
  135. /*****************************************************************************
  136. * DESCRIPTION:                                                               *
  137. * Routine to copy the control mesh lists to a trivariate control mesh.       *
  138. *   The trivariate is allocated here as well.                     *
  139. *   Returns the trivariate if o.k., otherwise NULL.                 *
  140. *                                                                            *
  141. * PARAMETERS:                                                                *
  142. *   LstLstObjList:  A list object of lists of lists of control points.       *
  143. *   UOrder:     U order of trivar.                                           *
  144. *   VOrder:     V order of trivar.                                           *
  145. *   WOrder:     V order of trivar.                                           *
  146. *   GType:      Geometry type - Bezier, Bspline etc.                         *
  147. *   ErrStr:     If an error, detected, this is initialized with description. *
  148. *                                                                            *
  149. * RETURN VALUE:                                                              *
  150. *   IPObjectStruct *:   A trivar object if successful, NULL otherwise.       *
  151. *****************************************************************************/
  152. static IPObjectStruct *GetControlTVMesh(IPObjectStruct *LstLstObjList,
  153.                     int UOrder,
  154.                     int VOrder,
  155.                     int WOrder,
  156.                     TrivGeomType GType,
  157.                     char **ErrStr)
  158. {
  159.     int i, j, k, l, PtSize, NumPlane,
  160.     NumVertices = 0,
  161.     NumVerticesFirst = -1,
  162.         NumLists = 0,
  163.     NumListsFirst = -1,
  164.         NumListLists = 0;
  165.     CagdRType **r;
  166.     RealType *v;
  167.     IPObjectStruct *TrivarObj, *LstObjList, *ObjList, *PtObj;
  168.     CagdPointType
  169.     PtType = CAGD_PT_E1_TYPE;
  170.  
  171.     while ((LstObjList = ListObjectGet(LstLstObjList, NumListLists)) != NULL) {
  172.     if (!IP_IS_OLST_OBJ(LstObjList)) {
  173.         *ErrStr = "Non list object found in list";
  174.         return NULL;
  175.     }
  176.  
  177.     while ((ObjList = ListObjectGet(LstObjList, NumLists)) != NULL) {
  178.         if (!IP_IS_OLST_OBJ(ObjList)) {
  179.         *ErrStr = "Non list object found in list";
  180.         return NULL;
  181.         }
  182.  
  183.         NumVertices = -1;
  184.         while ((PtObj = ListObjectGet(ObjList, ++NumVertices)) != NULL) {
  185.         if (!IP_IS_CTLPT_OBJ(PtObj) &&
  186.             !IP_IS_POINT_OBJ(PtObj) &&
  187.             !IP_IS_VEC_OBJ(PtObj)) {
  188.             *ErrStr = "Non point object found in list";
  189.             return NULL;
  190.         }
  191.         }
  192.         if ((PtType = IritPrsrCoerceCommonSpace(ObjList, PtType)) ==
  193.                                 CAGD_PT_NONE) {
  194.         *ErrStr = "";
  195.         return NULL;
  196.         }
  197.  
  198.         if (NumLists++ == 0 && NumListLists == 0)
  199.         NumVerticesFirst = NumVertices;
  200.         else if (NumVerticesFirst != NumVertices) {
  201.         *ErrStr = "Different size of point lists";
  202.         return NULL;
  203.         }
  204.     }
  205.  
  206.     if (NumListLists++ == 0)
  207.         NumListsFirst = NumLists;
  208.     else if (NumListsFirst != NumLists) {
  209.         *ErrStr = "Different size of list lists";
  210.         return NULL;
  211.     }
  212.     }
  213.  
  214.     /* Coerce all points to a common space, in place. */
  215.     while ((LstObjList = ListObjectGet(LstLstObjList, NumListLists)) != NULL)
  216.     while ((ObjList = ListObjectGet(LstObjList, NumLists)) != NULL)
  217.         if (IritPrsrCoercePtsListTo(ObjList, PtType) == CAGD_PT_NONE) {
  218.         *ErrStr = "";
  219.         return NULL;
  220.         }
  221.  
  222.     if (NumVertices < 2 || NumLists < 2 || NumListLists < 2) {
  223.     *ErrStr = "Less than 2 points in a row/col/depth";
  224.     return NULL;
  225.     }
  226.  
  227.     TrivarObj = GenTRIVARObject(NULL);
  228.     switch (GType) {
  229.     case TRIV_TVBEZIER_TYPE:
  230.         TrivarObj -> U.Trivars = TrivBzrTVNew(NumVertices, NumLists,
  231.                           NumListLists, PtType);
  232.         break;
  233.     case TRIV_TVBSPLINE_TYPE:
  234.         TrivarObj -> U.Trivars = TrivBspTVNew(NumVertices, NumLists,
  235.                           NumListLists, UOrder,
  236.                           VOrder, WOrder, PtType);
  237.         break;
  238.     default:
  239.         break;
  240.     }
  241.     AttrSetObjectColor(TrivarObj, GlblPrimColor);  /* Set its default color. */
  242.     PtSize = CAGD_IS_RATIONAL_PT(PtType) + CAGD_NUM_OF_PT_COORD(PtType);
  243.  
  244.     NumPlane = NumVertices * NumLists;
  245.     for (r = TrivarObj -> U.Trivars -> Points, i = 0; i < NumListLists; i++) {
  246.     LstObjList = ListObjectGet(LstLstObjList, i);
  247.  
  248.         for (j = 0; j < NumLists; j++) {
  249.         IPObjectStruct
  250.         *ObjList = ListObjectGet(LstObjList, j);
  251.  
  252.         for (k = 0; k < NumVertices; k++) {
  253.         IPObjectStruct
  254.             *VObj = ListObjectGet(ObjList, k);
  255.  
  256.         v = VObj -> U.CtlPt.Coords;
  257.  
  258.         if (CAGD_IS_RATIONAL_PT(PtType))
  259.             for (l = 0; l < PtSize; l++)
  260.             r[l][i * NumPlane + j * NumVertices + k] = *v++;
  261.         else
  262.             for (l = 1; l <= PtSize; l++)
  263.             r[l][i * NumPlane + j * NumVertices + k] = *++v;
  264.         }
  265.     }
  266.     }
  267.  
  268.     return TrivarObj;
  269. }
  270.  
  271. /*****************************************************************************
  272. * DESCRIPTION:                                                               M
  273. *   Routine to create a Bezier trivar geometric object defined by a list of  M
  274. * lists of lists of control points.                         M
  275. *                                                                            *
  276. * PARAMETERS:                                                                M
  277. *   LstLstObjList: A list object of lists of lists of control points.        M
  278. *                                                                            *
  279. * RETURN VALUE:                                                              M
  280. *   IPObjectStruct *: A Bezier trivar object if successful, NULL otherwise.  M
  281. *                                                                            *
  282. * KEYWORDS:                                                                  M
  283. *   GenBezierTrivarObject                                                    M
  284. *****************************************************************************/
  285. IPObjectStruct *GenBezierTrivarObject(IPObjectStruct *LstLstObjList)
  286. {
  287.     char *ErrStr, Line[LINE_LEN];
  288.     IPObjectStruct
  289.     *TrivObj = GetControlTVMesh(LstLstObjList, -1, -1, -1,
  290.                     TRIV_TVBEZIER_TYPE, &ErrStr);
  291.  
  292.     if (TrivObj == NULL) {
  293.     sprintf(Line, "TBEZIER: %s, empty object result.\n", ErrStr);
  294.     IritPrsrFatalError(Line);
  295.     }
  296.  
  297.     return TrivObj;
  298. }
  299.  
  300. /*****************************************************************************
  301. * DESCRIPTION:                                                               M
  302. *   Routine to create a Bspline trivar geometric object defined by a list    M
  303. * of lists of lists of control points.                         M
  304. *                                                                            *
  305. * PARAMETERS:                                                                M
  306. *   RUOrder:     U order of trivar.                                          M
  307. *   RVOrder:     V order of trivar.                                          M
  308. *   RWOrder:     W order of trivar.                                          M
  309. *   LstObjList:  A list object of lists of control points.                   M
  310. *   KntObjList:  A list of knots (numeric values).                           M
  311. *                                                                            *
  312. * RETURN VALUE:                                                              M
  313. *   IPObjectStruct *: A Bspline trivar object if successful, NULL otherwise. M
  314. *                                                                            *
  315. * KEYWORDS:                                                                  M
  316. *   GenBsplineTrivarObject                                                   M
  317. *****************************************************************************/
  318. IPObjectStruct *GenBsplineTrivarObject(RealType *RUOrder,
  319.                        RealType *RVOrder,
  320.                        RealType *RWOrder,
  321.                        IPObjectStruct *LstLstObjList,
  322.                        IPObjectStruct *KntObjList)
  323. {
  324.     int Len1, Len2, Len3,
  325.     UOrder = REAL_PTR_TO_INT(RUOrder),
  326.     VOrder = REAL_PTR_TO_INT(RVOrder),
  327.     WOrder = REAL_PTR_TO_INT(RWOrder);
  328.     char *ErrStr, Line[LINE_LEN];
  329.     IPObjectStruct
  330.     *TrivObj = GetControlTVMesh(LstLstObjList, UOrder, VOrder, WOrder,
  331.                     TRIV_TVBSPLINE_TYPE, &ErrStr);
  332.  
  333.     if (TrivObj == NULL) {
  334.     sprintf(Line, "TBSPLINE: Ctl mesh, %s, empty object result.\n", ErrStr);
  335.     IritPrsrFatalError(Line);
  336.     return NULL;
  337.     }
  338.  
  339.     if (!IP_IS_OLST_OBJ(KntObjList) || ListObjectLength(KntObjList) != 3) {
  340.     IPFreeObject(TrivObj);
  341.     IritPrsrFatalError("TBSPLINE: Exactly three knot vectors expected");
  342.     return NULL;
  343.     }
  344.  
  345.     if (TrivObj -> U.Trivars -> ULength < TrivObj -> U.Trivars -> UOrder ||
  346.     TrivObj -> U.Trivars -> VLength < TrivObj -> U.Trivars -> VOrder ||
  347.     TrivObj -> U.Trivars -> WLength < TrivObj -> U.Trivars -> WOrder) {
  348.     IPFreeObject(TrivObj);
  349.     IritPrsrFatalError("TBSPLINE: Trivar mesh length smaller than order.");
  350.     return NULL;
  351.     }
  352.  
  353.     IritFree((VoidPtr) TrivObj -> U.Trivars -> UKnotVector);
  354.     TrivObj -> U.Trivars -> UKnotVector = NULL;
  355.     IritFree((VoidPtr) TrivObj -> U.Trivars -> VKnotVector);
  356.     TrivObj -> U.Trivars -> VKnotVector = NULL;
  357.     IritFree((VoidPtr) TrivObj -> U.Trivars -> WKnotVector);
  358.     TrivObj -> U.Trivars -> WKnotVector = NULL;
  359.     Len1 = TrivObj -> U.Trivars -> ULength;
  360.     Len2 = TrivObj -> U.Trivars -> VLength;
  361.     Len3 = TrivObj -> U.Trivars -> WLength;
  362.     if ((TrivObj -> U.Trivars -> UKnotVector =
  363.      GetKnotVector(ListObjectGet(KntObjList, 0), UOrder,
  364.                &Len1, &ErrStr)) == NULL ||
  365.     (TrivObj -> U.Trivars -> VKnotVector =
  366.      GetKnotVector(ListObjectGet(KntObjList, 1), VOrder,
  367.                &Len2, &ErrStr)) == NULL ||
  368.     (TrivObj -> U.Trivars -> WKnotVector =
  369.      GetKnotVector(ListObjectGet(KntObjList, 2), WOrder,
  370.                &Len3, &ErrStr)) == NULL) {
  371.     sprintf(Line, "TBSPLINE: Knot vectors, %s, empty object result.\n",
  372.         ErrStr);
  373.     IPFreeObject(TrivObj);
  374.     IritPrsrFatalError(Line);
  375.     return NULL;
  376.     }
  377.  
  378.     if (Len1 != TrivObj -> U.Trivars -> ULength + UOrder) {
  379.     if (Len1 == TrivObj -> U.Trivars -> ULength + UOrder + UOrder - 1)
  380.         TrivObj -> U.Trivars -> UPeriodic = TRUE;
  381.     else {
  382.         IPFreeObject(TrivObj);
  383.         IritPrsrFatalError("Wrong knot vector length");
  384.         return NULL;
  385.     }
  386.     }
  387.     if (Len2 != TrivObj -> U.Trivars -> VLength + VOrder) {
  388.     if (Len2 == TrivObj -> U.Trivars -> VLength + VOrder + VOrder - 1)
  389.         TrivObj -> U.Trivars -> VPeriodic = TRUE;
  390.     else {
  391.         IPFreeObject(TrivObj);
  392.         IritPrsrFatalError("Wrong knot vector length");
  393.         return NULL;
  394.     }
  395.     }
  396.     if (Len3 != TrivObj -> U.Trivars -> WLength + WOrder) {
  397.     if (Len3 == TrivObj -> U.Trivars -> WLength + WOrder + WOrder - 1)
  398.         TrivObj -> U.Trivars -> WPeriodic = TRUE;
  399.     else {
  400.         IPFreeObject(TrivObj);
  401.         IritPrsrFatalError("Wrong knot vector length");
  402.         return NULL;
  403.     }
  404.     }
  405.  
  406.     return TrivObj;
  407. }
  408.  
  409. /*****************************************************************************
  410. * DESCRIPTION:                                                               M
  411. *   Evaluate a trivariate function, at the prescribed parametric location.   M
  412. *                                                                            *
  413. * PARAMETERS:                                                                M
  414. *   TVObj:    Trivariate to evaluate.                                        M
  415. *   u, v, w:  Parametric location to evaluate at.                            M
  416. *                                                                            *
  417. * RETURN VALUE:                                                              M
  418. *   IPObjectStruct *:  A control point of the same type TV has.              M
  419. *                                                                            *
  420. * KEYWORDS:                                                                  M
  421. *   EvalTrivarObject                                                         M
  422. *****************************************************************************/
  423. IPObjectStruct *EvalTrivarObject(IPObjectStruct *TVObj,
  424.                  RealType *u,
  425.                  RealType *v,
  426.                  RealType *w)
  427. {
  428.     CagdRType
  429.     *Pt = TrivTVEval(TVObj -> U.Trivars, *u, *v, *w);
  430.     IPObjectStruct
  431.     *CtlPtObj = GenCTLPTObject(TVObj -> U.Trivars -> PType, Pt, NULL);
  432.  
  433.     return CtlPtObj;
  434. }
  435.  
  436. /*****************************************************************************
  437. * DESCRIPTION:                                                               M
  438. *   Extracts an isoparametric surface out of the trivariate.                 M
  439. *                                                                            M
  440. *                                                                            *
  441. * PARAMETERS:                                                                M
  442. *   TVObj:     Trivariate to extract surface from.                           M
  443. *   RDir:      Direction of extarction. One of U, V, W.                      M
  444. *   ParamVal:  Parameter value of trivariate.                                M
  445. *                                                                            *
  446. * RETURN VALUE:                                                              M
  447. *   IPObjectStruct *:   An isoparametric surface.                            M
  448. *                                                                            *
  449. * KEYWORDS:                                                                  M
  450. *   SurfaceFromTrivar                                                        M
  451. *****************************************************************************/
  452. IPObjectStruct *SurfaceFromTrivar(IPObjectStruct *TVObj,
  453.                   RealType *RDir,
  454.                   RealType *ParamVal)
  455. {
  456.     int Dir = REAL_PTR_TO_INT(RDir);
  457.  
  458.     CagdSrfStruct
  459.     *Srf = TrivSrfFromTV(TVObj -> U.Trivars, *ParamVal, Dir);
  460.  
  461.     if (Srf == NULL)
  462.     return NULL;
  463.  
  464.     return GenSRFObject(Srf);
  465. }
  466.  
  467. /*****************************************************************************
  468. * DESCRIPTION:                                                               M
  469. * Routine to subdivide a trivariate function into two in specified direction M
  470. * (1 or 2) and specified parameter value.                     M
  471. *                                                                            *
  472. * PARAMETERS:                                                                M
  473. *   TVObj:        Trivariate to subdivide.                                   M
  474. *   RDir:         Direction of subdivision. Either U, V, or W.               M
  475. *   ParamVal:     Parameter value at which subdivision should occur.         M
  476. *                                                                            *
  477. * RETURN VALUE:                                                              M
  478. *   IPObjectStruct *:  A list object of two trivar objects, result of the    M
  479. *               subdivision.                         M
  480. *                                                                            *
  481. * KEYWORDS:                                                                  M
  482. *   DivideTrivarObject                                                       M
  483. *****************************************************************************/
  484. IPObjectStruct *DivideTrivarObject(IPObjectStruct *TVObj,
  485.                     RealType *RDir,
  486.                     RealType *ParamVal)
  487. {
  488.     int Dir = REAL_PTR_TO_INT(RDir);
  489.     TrivTVStruct
  490.     *TV = TrivTVSubdivAtParam(TVObj -> U.Trivars, *ParamVal, Dir);
  491.     IPObjectStruct *TV1, *TV2, *TVList;
  492.  
  493.     if (TV == NULL)
  494.     return NULL;
  495.  
  496.     TV1 = GenTRIVARObject(TV),
  497.     AttrSetObjectColor(TV1, AttrGetObjectColor(TVObj));
  498.     TV2 = GenTRIVARObject(TV -> Pnext),
  499.     AttrSetObjectColor(TV2, AttrGetObjectColor(TVObj));
  500.     TV -> Pnext = NULL;
  501.  
  502.     TVList = IPAllocObject("", IP_OBJ_LIST_OBJ, NULL);
  503.     ListObjectInsert(TVList, 0, TV1);
  504.     ListObjectInsert(TVList, 1, TV2);
  505.     ListObjectInsert(TVList, 2, NULL);
  506.  
  507.     return TVList;
  508. }
  509.  
  510. /*****************************************************************************
  511. * DESCRIPTION:                                                               M
  512. * Routine to extract a region of a trivariate in specified direction (1 or   M
  513. * 2) and specified parameter values.                         M
  514. *                                                                            *
  515. * PARAMETERS:                                                                M
  516. *   TVObj:       Trivariate to extract a region from.                        M
  517. *   RDir:        Direction of region extraction. Either U or V.              M
  518. *   ParamVal1:   Parameter of beginning of region.                           M
  519. *   ParamVal2:   Parameter of end of region.                                 M
  520. *                                                                            *
  521. * RETURN VALUE:                                                              M
  522. *   IPObjectStruct *:  A region of TVObj,                                    M
  523. *                                                                            *
  524. * KEYWORDS:                                                                  M
  525. *   RegionFromTrivarObject                                                   M
  526. *****************************************************************************/
  527. IPObjectStruct *RegionFromTrivarObject(IPObjectStruct *TVObj, 
  528.                     RealType *RDir,
  529.                     RealType *ParamVal1,
  530.                     RealType *ParamVal2)
  531. {
  532.     int Dir = REAL_PTR_TO_INT(RDir);
  533.     TrivTVStruct
  534.     *TV = TrivTVRegionFromTV(TVObj -> U.Trivars,
  535.                  *ParamVal1, *ParamVal2, Dir);
  536.  
  537.     if (TV == NULL)
  538.     return NULL;
  539.  
  540.     TVObj = GenTRIVARObject(TV);
  541.  
  542.     return TVObj;
  543. }
  544.  
  545. /*****************************************************************************
  546. * DESCRIPTION:                                                               M
  547. * Routine to refine a trivariate function in specified direction (1 or 2)    M
  548. * and knot vector.                                 M
  549. *   If, however, Replace is non zero, KnotsObj REPLACES current vector.      M
  550. *                                                                            *
  551. * PARAMETERS:                                                                M
  552. *   TVObj:      Trivariate to refine in direction RDir.                      M
  553. *   RDir:       Direction of refinement. Either U or V,                      M
  554. *   RReplace:   If TRUE KnotsObj will replace the RDir knot vector of TVObj. M
  555. *        Otherwise, the knots in KnotsObj will be added to it.         M
  556. *   KnotsObj:   A list of knots.                                             M
  557. *                                                                            *
  558. * RETURN VALUE:                                                              M
  559. *   IPObjectStruct *:   A refined trivar, or a trivar with a replaced knot   M
  560. *                       vector.                                              M
  561. *                                                                            *
  562. * KEYWORDS:                                                                  M
  563. *   RefineTrivarObject                                                       M
  564. *****************************************************************************/
  565. IPObjectStruct *RefineTrivarObject(IPObjectStruct *TVObj,
  566.                     RealType *RDir,
  567.                     RealType *RReplace,
  568.                     IPObjectStruct *KnotsObj)
  569. {
  570.     int n,
  571.     Replace = REAL_PTR_TO_INT(RReplace),
  572.     Dir = REAL_PTR_TO_INT(RDir);
  573.     char *ErrStr, Line[LINE_LEN];
  574.     CagdRType
  575.     *t = GetKnotVector(KnotsObj, 0, &n, &ErrStr);
  576.     TrivTVStruct *RefTV;
  577.     IPObjectStruct *RefTVObj;
  578.  
  579.     if (t == NULL) {
  580.     IPFreeObject(TVObj);
  581.     sprintf(Line, "REFINE: %s, empty object result.\n", ErrStr);
  582.     IritPrsrFatalError(Line);
  583.     return NULL;
  584.     }
  585.     RefTV = TrivTVRefineAtParams(TVObj -> U.Trivars, Dir, Replace, t, n);
  586.     IritFree((VoidPtr) t);
  587.     if (RefTV == NULL)
  588.     return NULL;
  589.  
  590.     RefTVObj = GenTRIVARObject(RefTV),
  591.     AttrSetObjectColor(RefTVObj, AttrGetObjectColor(TVObj));
  592.     return RefTVObj;
  593. }
  594.  
  595. /*****************************************************************************
  596. * DESCRIPTION:                                                               M
  597. * Routine to differentiate a trivariate function in Dir of SrfObj.         M
  598. *                                                                            *
  599. * PARAMETERS:                                                                M
  600. *   SrfObj:    Trivar to differentiate.                                      M
  601. *   Dir:       Direction of differentiation. Either U or V.                  M
  602. *                                                                            *
  603. * RETURN VALUE:                                                              M
  604. *   IPObjectStruct *:   A differentiated trivar.                             M
  605. *                                                                            *
  606. * KEYWORDS:                                                                  M
  607. *   DeriveTrivarObject                                                       M
  608. *****************************************************************************/
  609. IPObjectStruct *DeriveTrivarObject(IPObjectStruct *TVObj, RealType *Dir)
  610. {
  611.     TrivTVStruct
  612.     *DerivTV = TrivTVDerive(TVObj -> U.Trivars, REAL_PTR_TO_INT(Dir));
  613.     IPObjectStruct
  614.     *DerivTVObj = GenTRIVARObject(DerivTV);
  615.  
  616.     return DerivTVObj;
  617. }
  618.  
  619. /*****************************************************************************
  620. * DESCRIPTION:                                                               M
  621. *   Interpolates a three dimensional grid.                                   M
  622. *                                                                            *
  623. * PARAMETERS:                                                                M
  624. *   TVObj:     Trivariate to interpolate its control points.                 M
  625. *                                                                            *
  626. * RETURN VALUE:                                                              M
  627. *   IPObjectStruct *:   Interpolating trivar, with same order as original.   M
  628. *                                                                            *
  629. * KEYWORDS:                                                                  M
  630. *   InterpolateTrivar                                                        M
  631. *****************************************************************************/
  632. IPObjectStruct *InterpolateTrivar(IPObjectStruct *TVObj)
  633. {
  634.     TrivTVStruct
  635.     *NewTV = TrivInterpTrivar(TVObj -> U.Trivars);
  636.  
  637.     if (NewTV == NULL)
  638.     return NULL;
  639.  
  640.     return GenTRIVARObject(NewTV);
  641. }
  642.  
  643. /*****************************************************************************
  644. * DESCRIPTION:                                                               M
  645. *   Computes moments of freeform curves.                                     M
  646. *                                                                            *
  647. * PARAMETERS:                                                                M
  648. *   CrvObj:    Curve to compute moment for.                                  M
  649. *   RMoment:   Order of moment.                                              M
  650. *                                                                            *
  651. * RETURN VALUE:                                                              M
  652. *   IPObjectStruct *:   An E3 vector representing the approximated moment.   M
  653. *                                                                            *
  654. * KEYWORDS:                                                                  M
  655. *   ComputeCrvMoments, moments                                               M
  656. *****************************************************************************/
  657. IPObjectStruct *ComputeCrvMoments(IPObjectStruct *CrvObj, RealType *RMoment)
  658. {
  659.     CagdPType Pt;
  660.     CagdVType Vec;
  661.     int Moment = REAL_PTR_TO_INT(RMoment);
  662.  
  663.     CagdCrvFirstMoments(CrvObj -> U.Crvs, 100, Pt, Vec);
  664.  
  665.     switch (Moment) {
  666.     case 0:
  667.         return GenPTObject(&Pt[0], &Pt[1], &Pt[2]);
  668.     case 1:
  669.         return GenVECObject(&Vec[0], &Vec[1], &Vec[2]);
  670.     default:
  671.         IritPrsrFatalError("Moment: Only moments of order zero or one");
  672.         return NULL;
  673.     }
  674. }
  675.  
  676. /*****************************************************************************
  677. * DESCRIPTION:                                                               M
  678. * Construct a trivariate out of the provided surface list.             M
  679. *                                                                            *
  680. * PARAMETERS:                                                                M
  681. *   CrvList:     A list of surfaces to approximate a trivariate through.     M
  682. *   OtherOrder:  Other, third, order of trivariate.                          M
  683. *                                                                            *
  684. * RETURN VALUE:                                                              M
  685. *   IPObjectStruct *:   A trivariate approximately traversing through the    M
  686. *            given surfaces.                                      M
  687. *                                                                            *
  688. * KEYWORDS:                                                                  M
  689. *   GenTVFromSrfsObject                                                      M
  690. *****************************************************************************/
  691. IPObjectStruct *GenTVFromSrfsObject(IPObjectStruct *SrfList,
  692.                     RealType *OtherOrder)
  693. {
  694.     int i,
  695.     NumSrfs = 0;
  696.     IPObjectStruct *TVObj, *SrfObj, *PrevSrfObj, *HeadSrfObj;
  697.     TrivTVStruct *TV;
  698.     CagdSrfStruct *Srf,
  699.     *Srfs = NULL;
  700.  
  701.  
  702.     if (!IP_IS_OLST_OBJ(SrfList))
  703.     IritFatalError("TFROMSRF: Not object list object!");
  704.  
  705.     while ((SrfObj = ListObjectGet(SrfList, NumSrfs)) != NULL) {
  706.     if (!IP_IS_SRF_OBJ(SrfObj)) {
  707.         IritFatalError("TFROMSRF: List contains non surface object(s).");
  708.         return NULL;
  709.     }
  710.     if (SrfObj -> U.Srfs -> Pnext != NULL) {
  711.         IritFatalError("TFROMSRF: nested surface lists are disallowed.");
  712.         return NULL;
  713.     }
  714.     NumSrfs++;
  715.     }
  716.  
  717.     /* Chain all surfaces into a single list and invoke the TV constructor: */
  718.     HeadSrfObj = PrevSrfObj = ListObjectGet(SrfList, 0);
  719.     for (i = 0; i < NumSrfs; i++) {
  720.     SrfObj = ListObjectGet(SrfList, i);
  721.     Srf = CagdSrfCopy(SrfObj -> U.Srfs);
  722.     LIST_PUSH(Srf, Srfs);
  723.     }
  724.  
  725.     Srfs = CagdListReverse(Srfs);
  726.     TV = TrivTVFromSrfs(Srfs, REAL_PTR_TO_INT(OtherOrder));
  727.     CagdSrfFreeList(Srf);
  728.  
  729.     if (TV == NULL)
  730.     return NULL;
  731.  
  732.     TVObj = GenTRIVARObject(TV);
  733.  
  734.     return TVObj;
  735. }
  736.